import 'package:flutter_axrjc/datasource/network/api/LightApi.dart';
import 'package:flutter_axrjc/datasource/network/bean/AssetBean.dart';
import 'package:flutter_axrjc/datasource/network/bean/EventBean.dart';
import 'package:flutter_axrjc/datasource/network/bean/LightBean.dart';
import 'package:flutter_axrjc/datasource/network/bean/SelectOption.dart';
import 'package:flutter_axrjc/datasource/network/request/exp.dart';
import 'package:flutter_axrjc/main.dart';
import 'package:flutter_axrjc/widget/image_picker_dialog.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:image_cropper/image_cropper.dart';

import 'state.dart';

class EditLightLogic extends GetxController {
  final EditLightState state = EditLightState();
  late LightBean bean;
  @override
  void onInit() {
    super.onInit();
    bean = Get.arguments as LightBean;
    state.lux.value = bean.brightnessAdjustment ?? '';
    state.fixtureType.value = bean.fixtureType ?? '';
    state.fixtureName.value = bean.fixtureName ?? '';
    state.fixtureBrand.value = bean.fixtureBrand ?? '';
    // state.colorTemperatureAdjustment.value = bean.colorTemperatureAdjustment ?? '';
    state.fixtureModel.value = bean.fixtureModel ?? '';
    state.installDate.value = bean.installationDate ?? '';
    state.remark.value = bean.remark ?? '';
    getAllType();
    getImages("${bean.fixtureId}");
  }

  Future<void> getImages(String id) async {
    state.uploadedImgs.clear();
    state.uploadedImgs.addAll(await LightApi.getImage(id));
    refreshImg();
  }

  Future<void> getAllType() async {
    state.types.clear();
    state.types.addAll((await LightApi.getAllType())
        .map((e) => SelectOption("${e.codeName}", '${e.codeValue}'))
        .toList());
    state.fixtureTypeName.value = state.types
            .firstWhereOrNull((element) => bean.fixtureType == element.value)
            ?.label ??
        "";
  }

  Future<void> addImg() async {
    AssetBean? a = await showSingleMedia();
    if (a == null) {
      return;
    }

    CroppedFile? file = await ImageCropper().cropImage(sourcePath: a.path);
    if (file == null) {
      return;
    }
    state.localImageList.add(file.path);
    refreshImg();
  }

  void refreshImg() {
    state.imgList.clear();
    state.imgList.addAll(state.localImageList
        .map((e) => LightImageBean(
            isLocal: true,
            pathOrUrl: e,
            ifUploadedNo: '',
            ifUploadedBusinessNo: ''))
        .toList());
    state.imgList.addAll(state.uploadedImgs
        .map((e) => LightImageBean(
            isLocal: false,
            pathOrUrl: "${e.filePath}",
            ifUploadedNo: '${e.fileNo}',
            ifUploadedBusinessNo: '${e.businessNo}'))
        .toList());
    state.imgList.add(LightImageBean(
        isLocal: true,
        pathOrUrl: 'add',
        ifUploadedNo: '',
        ifUploadedBusinessNo: ''));
  }

  Future<void> change() async {
    try {
      if (state.localImageList.isNotEmpty) {
        print("开始上传图片");
        // state.localImageList
        await LightApi.addLightImage(state.localImageList, "${bean.fixtureId}");
      }
      await LightApi.change(
          brightnessAdjustment: state.lux.value,
          // colorTemperatureAdjustment: state.colorTemperatureAdjustment.value,
          fixtureBrand: state.fixtureBrand.value,
          // imgFile: state.localImageList,
          fixtureModel: state.fixtureModel.value,
          fixtureId: "${bean.fixtureId}",
          fixtureName: state.fixtureName.value,
          fixtureType: state.fixtureType.value,
          installationDate: state.installDate.value,
          remark: state.remark.value);
      eventBus.fire(EventBean(type: EventType.refreshLightImge, data: null));
      EasyLoading.showToast("修改成功");
      Get.back();
    } on ApiException catch (e) {
      EasyLoading.showToast(e.message);
    } catch (e) {
      print(e);
    }
  }

  Future<void> delete(LightImageBean bean) async {
    await LightApi.deleteImgByFileNo(bean.ifUploadedNo);
  }
}
